using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections;
using System.Diagnostics;
using NovodexWrapper;

/// <summary>
/// the ship is used to pick up marbles that fall off track
/// </summary>
/// 
namespace marbletrack
{
    public class Ship
    {
        public Vector3 modelPosition_;
        public Vector3 nextModelPosition_;
        private Vector3 modelRotation_;
        private Vector3 right;
        public Vector3 nextModelRotation_;
        protected ModelClass model_;
        public Ball ball_ = null;
        internal Matrix transform_;
        private Physics physics;
        internal Vector3 startingPosition = Game1.StartingPosition + new Vector3(0, Game1.SQUARESIZE * 2f, Game1.SQUARESIZE);
        internal float scale = 0.3f;
        public bool FlagDropBall = false;
        protected bool FlagGoingBack = false;
        protected bool FlagGetBall = false;
        protected bool FlagGotBall = false;

        protected int angleOfRotation = 0;
        protected int Angle//makes ship flyround in a circle
        {
            get
            {
                angleOfRotation++;
                if (angleOfRotation > 360)
                {
                    angleOfRotation = 0;
                }
                return angleOfRotation;
            }
        }

        public Ship()
        {
            model_ = Game1.ShipModel;
            modelPosition_ = startingPosition;
            nextModelPosition_ = modelPosition_;
            modelRotation_ = nextModelRotation_ = Vector3.Zero;
            physics = Game1.GetPhysics;
            model_.FlagTextured = true;
        }

        public void Update()
        {
            if (ball_ != null)
            {
                if (FlagGoingBack && Vector3.Distance(ball_.Position, startingPosition) <= 5)
                {
                    //drop ball at start
                    ball_.Actor.FlagDisableGravity = false;
                    ball_.Actor.FlagDisableCollision = false;
                    ball_.ship_ = null;
                    ball_ = null;
                    nextModelPosition_ = new Vector3(0f, 10000f, 0f);
                    FlagDropBall = true;
                }
                else if (!FlagDropBall && !FlagGoingBack  && !FlagGotBall && Vector3.Distance(modelPosition_, ball_.Position) > 5)
                {
                    //go pick up ball
                    nextModelPosition_ = ball_.Position;
                    FlagGetBall = true;
                }
                else if (!FlagDropBall && !FlagGoingBack && !FlagGotBall&& FlagGetBall && Vector3.Distance(modelPosition_, ball_.Position) < Game1.SQUARESIZE)
                {
                    FlagGotBall = true;
                }

                else if (!FlagDropBall && FlagGotBall && Vector3.Distance(ball_.Position, startingPosition) > 5)
                {
                    //got the ball going back to start
                    ball_.Actor.FlagDisableGravity = true;
                    ball_.Actor.FlagDisableCollision = true;
                    nextModelPosition_ = startingPosition + Vector3.UnitY * Game1.SQUARESIZE;
                    ball_.Position = modelPosition_ - Vector3.UnitY * Game1.SQUARESIZE;
                    FlagGoingBack = true;
                }
            }
            else
            {
                FlagDropBall = false;
                FlagGoingBack = false;
                FlagGetBall = false;
                FlagGotBall = false;
            
                nextModelPosition_.Y = startingPosition.Y;
                nextModelPosition_.X = startingPosition.X + (5000 * (float)(Math.Sin(MathHelper.ToRadians(Angle))));
                nextModelPosition_.Z = startingPosition.Z + (5000 * (float)(Math.Cos(MathHelper.ToRadians(Angle))));
            }

            if (Vector3.Distance(nextModelPosition_, modelPosition_) < Game1.SQUARESIZE)
            {
                modelPosition_ = nextModelPosition_;
            }
            else
            {
                modelPosition_ += (nextModelPosition_ - modelPosition_) / 30f;
            }

            modelRotation_ = nextModelPosition_-modelPosition_;
            right = Vector3.Cross(modelRotation_,Vector3.UnitY);
            modelRotation_.Normalize();
            right.Normalize();
            transform_ = Matrix.CreateTranslation(modelPosition_ / scale);
            //points ship in direction it is flying in
            transform_.Forward = modelRotation_;
            transform_.Right = right;
            transform_.Up = Vector3.Up;
            transform_ *= Matrix.CreateScale(scale);
        }

        public void Draw()
        {
            model_.Draw(transform_);
        }
    }
}